const babel = require('@babel/core')
const t = require('babel-types')

const visitor = {
  ImportDeclaration(nodePath,state) {
    let { node } = nodePath
    let { opts} = state
    let specifiers = node.specifiers
    let source = node.source
    if(opts.library === source.value && !t.isImportDefaultSpecifier(specifiers[0])){
      let importDeclarations = specifiers.map(specifier => {
        return t.importDeclaration([t.importDefaultSpecifier(specifier.local)], t.stringLiteral(`${source.value}/${specifier.imported.name}`))
      })
      if(importDeclarations.length === 1){
        nodePath.replaceWith(importDeclarations[0])
      }else{
        nodePath.replaceWithMultiple(importDeclarations)
      }
    }
  }
}

module.exports = function () {
  return {
    visitor
  }
}